﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SQLite;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace WorkSplit
{
    internal class SqliteHelper
    {
        static string connString = "DataSource=WorkSplitDB.db";
        /// <summary>
        /// 获取三段制程工序
        /// </summary>
        /// <returns></returns>
        public static Dictionary<DataTable,string> GetTable()
        {
            Dictionary<DataTable, string> res = new Dictionary<DataTable, string>();
            List<string> field = WorkNameGet();
            foreach (string v in field)
            {
                using (DataTable d=new DataTable(v))
                {
                    string sql = $"select 制程名称 from {v}";
                    using (SQLiteConnection conn=new SQLiteConnection(connString))
                    {
                        conn.Open();
                        using (SQLiteDataAdapter ad=new SQLiteDataAdapter(sql,conn))
                        {
                            ad.Fill(d);
                            res.Add(d,v);
                        }
                    }
                }
            }
            return res;
        }
        /// <summary>
        /// 获取表中表名，前中后三段
        /// </summary>
        /// <returns></returns>
        private static List<string> WorkNameGet()
        {
            List<string> res = new List<string>();
            using (DataTable t = new DataTable())
            {
                string sql = "select tbl_name from sqlite_master";
               // string sql = "select * from Qian";
                using (SQLiteConnection conn = new SQLiteConnection(connString))
                {
                    conn.Open();
                    using (SQLiteDataAdapter ad = new SQLiteDataAdapter(sql, conn))
                    {
                        ad.Fill(t);
                        foreach (DataRow row in t.Rows)
                        {
                            res.Add(row.ItemArray[0].ToString());
                        }
                        return res;
                    }
                }
            }
           
        }
    }
}
